package com.fivefu.core.camera.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fivefu.base.common.utils.date.DateUtils;
import com.fivefu.core.camera.entity.DbMonitorEventExamine;
import com.fivefu.core.camera.mapper.DbMonitorEventExamineMapper;
import com.fivefu.core.camera.services.DbMonitorEventExamineService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fivefu.core.camera.services.DbMonitorStatusService;
import com.fivefu.core.skyeye.event.entity.DbAiEvent;
import com.fivefu.core.skyeye.event.entity.DbAiScene;
import com.fivefu.core.skyeye.event.services.DbAiEventService;
import com.fivefu.core.skyeye.event.services.DbAiSceneService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 事件审核表 服务实现类
 * </p>
 *
 * @author 
 * @since 2022-09-21 19:37:01
 */
@Service
public class DbMonitorEventExamineServiceImpl extends ServiceImpl<DbMonitorEventExamineMapper, DbMonitorEventExamine> implements DbMonitorEventExamineService {

    @Autowired
    private DbAiSceneService dbAiSceneService;

    @Autowired
    private DbAiEventService dbAiEventService;

    @Autowired
    private DbMonitorStatusService dbMonitorStatusService;

    /**
     * 查询事件审核信息
     * @param eventId 事件编号
     * @return
     */
    @Override
    public DbMonitorEventExamine queryExamineByEventId(Long eventId){
        QueryWrapper<DbMonitorEventExamine> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        wrapper.eq("event_id",eventId);
        DbMonitorEventExamine examine = baseMapper.selectOne(wrapper);
        return examine;
    }

    /**
     * 事件审核
     * @param eventExamine 审核信息
     * @param userid 操作用户编号
     */
    @Override
    public void examineEvent(DbMonitorEventExamine eventExamine, String userid){
        LocalDateTime time = LocalDateTime.now();
        eventExamine.setCreatedBy(userid);
        eventExamine.setCreatedTime(time);
        eventExamine.setUpdatedBy(userid);
        eventExamine.setUpdatedTime(time);
        eventExamine.setIsDelete(0);
        baseMapper.insert(eventExamine);
    }

    /**
     * 格式化审核信息
     * @param examine 审核信息
     * @return
     */
    @Override
    public Map<String, Object> formatEventExamine(DbMonitorEventExamine examine){
        Map<String,Object> map = new HashMap<>();
        map.put("eventId", examine.getEventId());//事件编号
        map.put("examineUser", examine.getExamineUser());//审核人
        if (examine.getExamineTime() != null){
            map.put("examineTime", DateUtils.formatNormal(examine.getExamineTime()));//审核时间
        }else {
            map.put("examineTime", "");//审核时间
        }
        map.put("examineStatus",examine.getExamineStatus());//审核状态
        if (examine.getExamineStatus() != null){
            String statusName = dbMonitorStatusService.queryStatusNameById(examine.getExamineStatus());
            map.put("examineStatusName",statusName);//审核状态
        }
        map.put("examineReason",examine.getExamineReason());//理由
        return map;
    }

    /**
     *
     * 所有审核信息
     * @param examineStatus 审核状态
     */
    @Override
    public List<DbMonitorEventExamine> queryEventExamineList(Long examineStatus) {
        QueryWrapper<DbMonitorEventExamine> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        if (examineStatus != null){
            wrapper.eq("examine_status",examineStatus);
        }
        return baseMapper.selectList(wrapper);
    }

    /**
     *
     * 将审核数据转化为 以 scenecode为key，List<DbMonitorEventExamine>为值的map
     * @param list
     */
    @Override
    public Map<String, List<DbMonitorEventExamine>> sceneCodeAndEventExamineList(List<DbMonitorEventExamine> list) {
        Map<String, List<DbMonitorEventExamine>> map = new HashMap<>();

        for (DbMonitorEventExamine dbMonitorEventExamine : list) {
            //事件信息
            DbAiEvent dbAiEvent = dbAiEventService.queryEventById(dbMonitorEventExamine.getId());
            if (dbAiEvent != null){
                //场景信息
                DbAiScene dbAiScene = dbAiSceneService.querySceneByCode(dbAiEvent.getSceneId());
                //为map赋值
                if (dbAiScene != null && map.containsKey(dbAiScene.getCode())){
                    List<DbMonitorEventExamine> examineList = map.get(dbAiScene);
                    examineList.add(dbMonitorEventExamine);
                }else if (dbAiScene != null && !map.containsKey(dbAiScene.getCode())){
                    List<DbMonitorEventExamine> examines = new ArrayList<>();
                    examines.add(dbMonitorEventExamine);
                    map.put(dbAiScene.getCode(),examines);
                }
            }
        }

        return map;
    }

    /**
     *
     * 通过事件id集合查询审核信息
     * @param eventIds 事件id集合
     */
    @Override
    public List<DbMonitorEventExamine> queryEventExaminesByEventIds(List<Long> eventIds) {
        QueryWrapper<DbMonitorEventExamine> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        wrapper.in("event_id",eventIds);
        return baseMapper.selectList(wrapper);
    }

    /**
     *
     *通过审核状态查询事件编号
     * @param examineStatus 审核状态
     */
    @Override
    public List<Long> queryEventIdByExamineStatus(Long examineStatus) {
        List<Long> eventIds = new ArrayList<>();

        List<DbMonitorEventExamine> examineList = queryEventExamineList(examineStatus);;
        if (examineList != null && examineList.size() > 0){
            eventIds = examineList.stream().map(k -> k.getEventId()).collect(Collectors.toList());
        }

        return eventIds;
    }
}
